package org.fu.crowd.mvc.controller;

import org.fu.crowd.entity.Role;
import org.fu.crowd.service.api.AdminService;
import org.fu.crowd.service.api.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class AssignRoleController {

    @Autowired
    AdminService adminService;

    @Autowired
    RoleService roleService;


    /**
     *                  查询已分配角色和未分配角色，并跳转到指定页面
     * @param adminId
     * @param model
     * @return
     */
    @RequestMapping("/assign/toAssign")
    public String toAssignRolePage(@RequestParam("adminId")Integer adminId, Model model){
        //查询已分配角色
        List<Role> assignRole = roleService.getAssignRole(adminId);
        //查询未分配角色
        List<Role> unAssignRole = roleService.getUnAssignRole(adminId);
        model.addAttribute("assignRole",assignRole);
        model.addAttribute("unAssignRole",unAssignRole);
        return "assignRole";
    }


    /**
     * 保存重新分配后的的角色
     * @return
     */
    @RequestMapping("/assign/doRoleAssign")
    public String saveRelationship(@RequestParam("adminId")Integer adminId,
                                   @RequestParam("pageNum")Integer pageNum,
                                   @RequestParam("keyword")String keyword,
                                   //可以没有值
                                   @RequestParam(value = "roleIdList",required = false)List<Integer> roleIdList){

        roleService.saveRelationship(adminId,roleIdList);
        System.out.println("进来了");
        return "redirect:/main/mainuser?keyword="+keyword+"&pageNum="+pageNum;
    }


}
